using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ASP_TF2LogParser.UserControls
{
    public partial class PlayerVictimControlByLogId : System.Web.UI.UserControl
    {

        //private LogParser.DA.PlayerModel _model;
        //string _playerId;

        protected void Page_Load( object sender, EventArgs e )
        {

            //_playerId = Request[ "playerId" ].ToString();
            //_model = new LogParser.DA.PlayerModel();


            //LogParser.DA.DataObjects.PlayerStatsDS.PlayerVictimsSelectByPlayerIdDataTable pvdt = _model.GetPlayerVictims( _playerId );

            //if ( !Page.IsPostBack )
            //    pvdt.DefaultView.Sort = "Kills DESC";

            //this.playerVictimsGridView.DataSource = pvdt;
            //this.playerVictimsGridView.DataBind();

            string playerId = Request.QueryString[ "playerId" ];

            string logIdList = string.Empty;

            int leagueId = Convert.ToInt32( Request[ "leagueId" ] );
            List<int> logList = LogParser.DA.LogFileModel.GetLogIdList( leagueId );

            foreach ( int logId in logList )
            {
                if ( logIdList != string.Empty )
                    logIdList += ",";

                logIdList += logId.ToString();
            }
            

                if ( logIdList != string.Empty )
            {
                NoLeagueIdErrorMessage.Visible = false;
                PlayerVictimDataDivId.Visible = true;
                LoadPlayerVictims( playerId, logIdList );
            }
            else
            {
                NoLeagueIdErrorMessage.Visible = true;
                PlayerVictimDataDivId.Visible = false;
            }


        }

        
        private void LoadPlayerVictims( string playerId, string logIdList )
        {
            LogParser.DA.DataObjects.PlayerStatsDS.PlayerVictimsSelectByPlayerIdDataTable dt = LogParser.DA.PlayerModel.GetPlayerVictims( playerId, logIdList );

            playerVictimsGridView.DataSource = dt;
            playerVictimsGridView.DataBind();
        }

        public string GenerateUrl( string playerName, string playerId )
        {

            string returnUrl = "";

            returnUrl = "<a href='Player.aspx?playerId=" + playerId + "'>" + playerName + "</a>";

            return returnUrl;


        }

        #region Sorting

        protected void playerVictimStats_Sorting( object sender, GridViewSortEventArgs e )
        {
            DataTable dataTable = playerVictimsGridView.DataSource as DataTable;

            if ( dataTable != null )
            {
                //DataView dataView = new DataView( dataTable );
                //dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql( GridViewSortDirection );


                dataTable.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql( GridViewSortDirection );
                playerVictimsGridView.DataSource = dataTable;
                playerVictimsGridView.DataBind();
            }



        }

        public SortDirection GridViewSortDirection
        {
            get
            {
                if ( ViewState[ "sortDirection" ] == null )
                    ViewState[ "sortDirection" ] = SortDirection.Ascending;

                SortDirection returnDirection = ( SortDirection ) ViewState[ "sortDirection" ];

                if ( ( SortDirection ) ViewState[ "sortDirection" ] == SortDirection.Ascending )
                    ViewState[ "sortDirection" ] = SortDirection.Descending;
                else
                    ViewState[ "sortDirection" ] = SortDirection.Ascending;



                return returnDirection;
            }
            set { ViewState[ "sortDirection" ] = value; }
        }

        private string ConvertSortDirectionToSql( SortDirection sortDirection )
        {
            string newSortDirection = String.Empty;

            switch ( sortDirection )
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }

        #endregion


    }
}